/*
 * rx_functions.c
 * Created on July 8, 2005; 4:22 PM
 * by VRamadurai
 */
#include <stdio.h>
#include <stdlib.h>

/************ Private Header Files **************/
#include "HsTypes.h"
#include "PhyDebug.h"
#include "PhyStruct.h"
#include "PmdGlobals.h"
#include "PhyConstants.h"
#include "rx_functions.h"
#include "rx1_functions.h"
#include "rx2_functions.h"
#include "equalizer.h"
#include "thread_globals.h"
#include "threads_common.h"//add 627

void
wlan_do_2mbps(
                struct HS_Phy11bStr  *HS_Phy11bPtrTh,
                ePHYSTATES_TH *ePhyState,
                hs_int32_t NSymbols,
                hs_int16_t *ssSineCosineTable
             )
{
    #define NUM_ITER    3

    hs_int32_t i;
    hs_int16_t ssInI[96+32];
    hs_int16_t ssInQ[96+32];
    hs_int16_t cos_fixed[32];
    hs_int16_t sin_fixed[32];
    hs_int16_t tempI[96];
    hs_int16_t tempQ[96];

    hs_int16_t *ssOutI;
    hs_int16_t *outIBase;
    hs_int16_t *sincos_t;

    hs_int32_t outbuff_toggle;
    hs_uint32_t uiRotorTableIndex;
    hs_int32_t siRotorTableCounter;
    hs_int16_t cos_val;
    hs_int16_t sin_val;

    hs_int32_t symbol_count;
    hs_int32_t count;

    hs_int32_t siPhaseUpdate = HS_Phy11bPtrTh->siPhaseUpdate;
    volatile hs_int32_t *thread1to2_flag1_ptr = &thread1to2_flag1;

    sincos_t = HS_Phy11bPtrTh->sincos_t;

    outIBase = ssOutIQ_shared;
    outbuff_toggle = HS_Phy11bPtrTh->outbuff_toggle_th1;
    siRotorTableCounter = HS_Phy11bPtrTh->siRotorTableCounter;

    symbol_count = 0;

    while(thread3toall_rate_flag == 0){
		__sb_barrier();
    }

    if(*HS_Phy11bPtrTh->rx_status_l == HS_HEAD_CRC_ERROR){
    	*ePhyState = PHY_STATE_SWITCH_OFF_TH;
    	return;
    }

    while(1)
    {
        sync_on_dma((hs_int32_t)HS_Phy11bPtrTh->piRdPtrI, 176);

        START_CLOCK

        rx_copysamps88_update(HS_Phy11bPtrTh, ssInI, ssInQ);

        for(i=0; i<NUM_ITER; i++){
            __sb_rshf1(tempI+i*16, tempI+i*16+48, ssInI+i*32, ssInI+i*32+16, 7);
            __sb_rshf1(tempQ+i*16, tempQ+i*16+48, ssInQ+i*32, ssInQ+i*32+16, 7);
        }

        ssOutI = outIBase + outbuff_toggle*OUTBUFFSIZE*2;
        outbuff_toggle ^= 1;

        for(count=0; count<NUM_ITER; count++)
        {
            uiRotorTableIndex =(((siRotorTableCounter+ 0x200000)&0x7fffffff) >> 22);
            cos_val = ssSineCosineTable[uiRotorTableIndex];
            sin_val = ssSineCosineTable[513+uiRotorTableIndex];

            __sb_rbroad(cos_fixed, (cos_val<<16), 0);
            __sb_rbroad(sin_fixed, (sin_val<<16), 0);

            __sb_rmuls(ssInI, tempI+count*16, sincos_t);
            __sb_rmacs(ssInI, tempQ+count*16, sincos_t+32, ssInI);
            __sb_rmuls(ssInQ, tempI+count*16, sincos_t+32);
            __sb_rmsubs(ssInQ, tempQ+count*16, sincos_t, ssInQ);

            __sb_rmuls(tempI, ssInI, cos_fixed);
            __sb_rmacs(ssOutI+count*16, ssInQ, sin_fixed, tempI);
            __sb_rmuls(tempQ, ssInI, sin_fixed);
            __sb_rmsubs(ssOutI+OUTBUFFSIZE+count*16, ssInQ, cos_fixed, tempQ);

            siRotorTableCounter += (2*siPhaseUpdate*16);
        }

        siRotorTableCounter -= (2*siPhaseUpdate*4);

        /** sync with thread2 **/
        while(*thread1to2_flag1_ptr == 1){
            __sb_barrier();
        }

        *thread1to2_flag1_ptr = 1;

        symbol_count+=4;
        if(symbol_count >= NSymbols){
            *ePhyState = PHY_STATE_SWITCH_OFF_TH;
            break;
        }
    }

    PRINT_CYCLES("data 2mbps 1");
}



